﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Routing;
using BUS;
using System.Configuration;
using System.Data.SqlClient;

public partial class UControl_uc_main_result : System.Web.UI.UserControl
{
    private string entry = "";
    private string catelvid = "";
    string currentpage = "1";
    BUS_WRENTRY busWrentry = new BUS_WRENTRY();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["currentpage"] != null)
        {
            currentpage = Session["currentpage"].ToString();
        }
        if (Convert.ToInt32(Session["City"]) > 0)
        {
            if (!IsPostBack)
            {
                string idcity = Session["City"].ToString();
                catelvid = Session["CateID"].ToString();
                ShowEntry(idcity, catelvid, 1, 20);
                ShowPaging(idcity, catelvid);
            }
        }
        else
        {
            string listid = Session["ListCity"].ToString();
            if (listid == "")
            {
                Response.Redirect("~/Default.aspx");
            }
            catelvid = Session["CateID"].ToString();
            ShowEntry_all(listid, catelvid, 1, 20);
            //Response.Redirect("~/Default.aspx");
        }
    }

    public string str_con = ConfigurationManager.ConnectionStrings["SQlConnection"].ToString();

    public DataSet query_sql(string idcity, int idcatelv, int from, int to)
    {
        string sql = "";
        if (idcatelv == 0)
        {
            sql = "select * from "
                       + "     (select en.ID, en.EntryName, "
                       + "     (select est.TypeSaleName from [dbo].[WRENTSALETYPE] est where est.ID = en.IDSaleType) as EntSaleType, "
                       + "     (select et.EntryTypeName from [dbo].[WRENTTYPE] et where et.ID = en.IDEntType) as EntType, en.Price, "
                       + "     (select cate.CategoryLv1Name from [dbo].[WRCATEGORYLV1] cate where cate.ID = en.IDCategoryLv) as CateLV1, "
                       + "     IsNull((select top(1) store.MediaPath from [dbo].[WRMEDIASTORE] store where store.IDEntry = en.ID and store.Active = 'Y'),'../Images/Pic/no-image.png') as MediaPath, "
                       + "     DATEDIFF (minute, en.UpdateDate, getdate ()) AS [Minute], "
                       + "     ('/' + cast(en.ID as varchar) + '/ViewEntry') as Link, "
                       + "     ROW_NUMBER() OVER (ORDER BY ID) as ROWNUM, "
                       + "     en.Active,en.EntContent "
                       + "     from [dbo].[WRENTRY] en "
                       + "     where en.IDCity in(" + idcity + ") and en.Active = 'Y' and en.IDUrgent = 2) p "
                       + "     where p.ROWNUM between " + from + " and " + to + " ";
        }
        else
        {
            sql = "select * from "
                + "        (select en.ID, en.EntryName, "
                + "        (select est.TypeSaleName from [dbo].[WRENTSALETYPE] est where est.ID = en.IDSaleType) as EntSaleType, "
                + "        (select et.EntryTypeName from [dbo].[WRENTTYPE] et where et.ID = en.IDEntType) as EntType, en.Price, "
                + "        (select cate.CategoryLv1Name from [dbo].[WRCATEGORYLV1] cate where cate.ID = en.IDCategoryLv) as CateLV1, "
                + "        IsNull((select top(1) store.MediaPath from [dbo].[WRMEDIASTORE] store where store.IDEntry = en.ID and store.Active = 'Y'),'../Images/Pic/no-image.png') as MediaPath, "
                + "        DATEDIFF (minute, en.UpdateDate, getdate ()) AS [Minute], "
                + "        ('/' + cast(en.ID as varchar) + '/ViewEntry') as Link, "
                + "        ROW_NUMBER() OVER (ORDER BY ID) as ROWNUM, "
                + "         en.Active,en.EntContent "
                + "        from [dbo].[WRENTRY] en "
                + "        where en.IDCategoryLv = " + idcatelv + " and en.IDCity in (" + idcity + ") and en.Active = 'Y' and en.IDUrgent = 2) p "
                + "        where p.ROWNUM between " + from + " and " + to + "  ";
        }
        DataSet ds = new DataSet();
        SqlConnection oConnection = new SqlConnection(str_con);
        SqlCommand oCommand = new SqlCommand(sql, oConnection);
        oCommand.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = oCommand;
        da.Fill(ds);

        oConnection.Close();
        oConnection.Dispose();
        oConnection = null;

        return ds;
    }

    public DataSet query_sql_UT(string idcity, int idcatelv, int from, int to)
    {
        string sql = "";
        if (idcatelv == 0)
        {
            sql = "select * from "
                       + "     (select en.ID, en.EntryName, "
                       + "     (select est.TypeSaleName from [dbo].[WRENTSALETYPE] est where est.ID = en.IDSaleType) as EntSaleType, "
                       + "     (select et.EntryTypeName from [dbo].[WRENTTYPE] et where et.ID = en.IDEntType) as EntType, en.Price, "
                       + "     (select cate.CategoryLv1Name from [dbo].[WRCATEGORYLV1] cate where cate.ID = en.IDCategoryLv) as CateLV1, "
                       + "     IsNull((select top(1) store.MediaPath from [dbo].[WRMEDIASTORE] store where store.IDEntry = en.ID and store.Active = 'Y'),'../Images/Pic/no-image.png') as MediaPath, "
                       + "     DATEDIFF (minute, en.UpdateDate, getdate ()) AS [Minute], "
                       + "     ('/' + cast(en.ID as varchar) + '/ViewEntry') as Link, "
                       + "     ROW_NUMBER() OVER (ORDER BY ID) as ROWNUM, "
                       + "     en.Active,en.EntContent "
                       + "     from [dbo].[WRENTRY] en "
                       + "     where en.IDCity in(" + idcity + ") and en.Active = 'Y' and en.IDUrgent = 1) p "
                       + "     where p.ROWNUM between " + from + " and " + to + " ";
        }
        else
        {
            sql = "select * from "
                + "        (select en.ID, en.EntryName, "
                + "        (select est.TypeSaleName from [dbo].[WRENTSALETYPE] est where est.ID = en.IDSaleType) as EntSaleType, "
                + "        (select et.EntryTypeName from [dbo].[WRENTTYPE] et where et.ID = en.IDEntType) as EntType, en.Price, "
                + "        (select cate.CategoryLv1Name from [dbo].[WRCATEGORYLV1] cate where cate.ID = en.IDCategoryLv) as CateLV1, "
                + "        IsNull((select top(1) store.MediaPath from [dbo].[WRMEDIASTORE] store where store.IDEntry = en.ID and store.Active = 'Y'),'../Images/Pic/no-image.png') as MediaPath, "
                + "        DATEDIFF (minute, en.UpdateDate, getdate ()) AS [Minute], "
                + "        ('/' + cast(en.ID as varchar) + '/ViewEntry') as Link, "
                + "        ROW_NUMBER() OVER (ORDER BY ID) as ROWNUM, "
                + "         en.Active,en.EntContent "
                + "        from [dbo].[WRENTRY] en "
                + "        where en.IDCategoryLv = " + idcatelv + " and en.IDCity in (" + idcity + ") and en.Active = 'Y' and en.IDUrgent = 1) p "
                + "        where p.ROWNUM between " + from + " and " + to + "  ";
        }
        DataSet ds = new DataSet();
        SqlConnection oConnection = new SqlConnection(str_con);
        SqlCommand oCommand = new SqlCommand(sql, oConnection);
        oCommand.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = oCommand;
        da.Fill(ds);

        oConnection.Close();
        oConnection.Dispose();
        oConnection = null;

        return ds;
    }

    private void ShowEntry_all(string idcity, string idcatelv1, int from, int to)
    {
        if (idcatelv1 == string.Empty)
        {
            //lấy tất cả catelv1
            //DataTable dt = busWrentry.Get_Entry_View_all(idcity, 0, from, to);
            DataSet ds = query_sql(idcity, 0, from, to);
            lsvSanPham.DataSource = ds.Tables[0];
            lsvSanPham.DataBind();

            //DataTable dt_UT = busWrentry.Get_Entry_View_all_UT(idcity, 0, from, to);
            DataSet ds_UT = query_sql_UT(idcity, 0, from, to);
            lsvSanPhamUT.DataSource = ds_UT.Tables[0];
            lsvSanPhamUT.DataBind();
        }
        else
        {
            // lấy theo catelv1
            //DataTable dt = busWrentry.Get_Entry_View_all(idcity, Int32.Parse(idcatelv1), from, to);
            DataSet ds = query_sql(idcity, Int32.Parse(idcatelv1), from, to);
            lsvSanPham.DataSource = ds.Tables[0];
            lsvSanPham.DataBind();
            //DataTable dt_UT = busWrentry.Get_Entry_View_all_UT(idcity, Int32.Parse(idcatelv1), from, to);
            DataSet ds_UT = query_sql_UT(idcity, Int32.Parse(idcatelv1), from, to);
            lsvSanPhamUT.DataSource = ds_UT.Tables[0];
            lsvSanPhamUT.DataBind();
        }
    }

    private void ShowPaging(string idcity, string idcatelv1)
    {
        string truoc = ""; string sau = "";
        if (Session["SelLang"].ToString() == "vi")
        {
            truoc = "Trước";
            sau = "Sau";
        }
        else
        {
            truoc = "Prev";
            sau = "Next";
        }

        bool flag = false;
        string chuoi = "";
        chuoi += "<ul>";
        if (busWrentry._Count() > 10)
        {
            if ((busWrentry._Count()) % 10 == 0)
            {
                if (Int32.Parse(currentpage) == 1)
                {
                    chuoi += "<li><a class='li_first' href='../1/Page/ViewEntry.html'>" + truoc + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_first' href='../" + (Int32.Parse(currentpage) - 1).ToString() + "/Page/ViewEntry.html'>" + truoc + "</a></li>";
                }
                for (int i = 1; i <= (busWrentry._Count() / 10); i++)
                {

                    if (i % 11 == 0 && Int32.Parse(currentpage) % 10 != 0)
                    {
                        break;
                    }
                    if (Int32.Parse(currentpage) % 10 == 0)
                    {
                        if (flag)
                            goto here;
                        flag = true;
                        i = Int32.Parse(currentpage);
                    }
                here:
                    if (i.ToString().Equals(currentpage))
                    {
                        chuoi += "<li><a class='selected' href='../" + i.ToString() + "/Page/ViewEntry.html'>" + i + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a  href='../" + i.ToString() + "/Page/ViewEntry.html'>" + i + "</a></li>";
                    }
                }
                if (Int32.Parse(currentpage) == ((busWrentry._Count() / 10) + 1))
                {
                    chuoi += "<li><a class='li_last' href='../" + currentpage + "/Page/ViewEntry.html'>" + sau + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_last' href='/" + (Int32.Parse(currentpage) + 1).ToString() + "/Page/ViewEntry.html'>" + sau + "</a></li>";
                }
            }
            else
            {
                if (Int32.Parse(currentpage) == 1)
                {
                    chuoi += "<li><a class='li_first' href='../1/Page/ViewEntry.html'>" + truoc + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_first' href='../" + (Int32.Parse(currentpage) - 1).ToString() + "/Page/ViewEntry.html'>" + truoc + "</a></li>";
                }

                for (int i = 1; i <= (busWrentry._Count() / 10) + 1; i++)
                {
                    if (i % 11 == 0 && Int32.Parse(currentpage) % 10 != 0)
                    {
                        break;
                    }
                    if (Int32.Parse(currentpage) % 10 == 0)
                    {
                        if (flag)
                            goto here;
                        flag = true;
                        i = Int32.Parse(currentpage);
                    }
                here:
                    if (i.ToString().Equals(currentpage))
                    {
                        chuoi += "<li><a class='selected' href='../" + i.ToString() + "/Page/ViewEntry.html'>" + i + "</a></li>";
                    }
                    else
                    {
                        chuoi += "<li><a href='../" + i.ToString() + "/Page/ViewEntry.html'>" + i + "</a></li>";
                    }
                }
                if (Int32.Parse(currentpage) == ((busWrentry._Count() / 10) + 1))
                {
                    chuoi += "<li><a class='li_last' href='../" + currentpage + "/Page/ViewEntry.html'>" + sau + "</a></li>";
                }
                else
                {
                    chuoi += "<li><a class='li_last' href='../" + (Int32.Parse(currentpage) + 1).ToString() + "/Page/ViewEntry.html'>" + sau + "</a></li>";
                }
            }
        }
        chuoi += "</ul>";
        ltrpaging.Text = chuoi;
        if (idcatelv1 == string.Empty)
        {
            lsvSanPhamUT.DataSource = busWrentry.Get_Entry_View_UT(Int32.Parse(idcity), 0, Int32.Parse(currentpage) * 10 - 10 + 1, Int32.Parse(currentpage) * 10);
            lsvSanPhamUT.DataBind();
        }
        else
        {
            lsvSanPhamUT.DataSource = busWrentry.Get_Entry_View_UT(Int32.Parse(idcity), Int32.Parse(idcatelv1), Int32.Parse(currentpage) * 10 - 10 + 1, Int32.Parse(currentpage) * 10);
            lsvSanPhamUT.DataBind();
        }
    }

    private void ShowEntry(string idcity, string idcatelv1, int from, int to)
    {
        if (idcatelv1 == string.Empty)
        {
            //lấy tất cả catelv1
            DataTable dt = busWrentry.Get_Entry_View(Int32.Parse(idcity), 0, from, to);
            lsvSanPham.DataSource = dt;
            lsvSanPham.DataBind();
            //DataTable dt_UT = busWrentry.Get_Entry_View_UT(Int32.Parse(idcity), 0, from, to);
            //lsvSanPhamUT.DataSource = dt_UT;
            //lsvSanPhamUT.DataBind();
        }
        else
        {
            // lấy theo catelv1
            DataTable dt = busWrentry.Get_Entry_View(Int32.Parse(idcity), Int32.Parse(idcatelv1), from, to);
            lsvSanPham.DataSource = dt;
            lsvSanPham.DataBind();
            //DataTable dt_UT = busWrentry.Get_Entry_View_UT(Int32.Parse(idcity), Int32.Parse(idcatelv1), from, to);
            //lsvSanPhamUT.DataSource = dt_UT;
            //lsvSanPhamUT.DataBind();
        }

    }
}